package chatin.client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.xmlrpc.XmlRpcException;
import javax.swing.SwingConstants;
import java.awt.Font;

public class LoginUI extends JPanel {

	private JPanel userPanel = null;
	private JTextField usernameField = null;
	private JLabel usernameLabel = null;
	private JLabel passwordLabel = null;
	private JLabel alertLabel = null;
	private JButton loginButton = null;
	private JPasswordField passwordField = null;
	private JPanel passPanel = null;
	private JPanel commandPanel = null;
	private JPanel groupPanel = null;
	/**
	 * This method initializes
	 * 
	 */
	public LoginUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		GridLayout gridLayout3 = new GridLayout();
		gridLayout3.setRows(5);
		gridLayout3.setColumns(3);
		this.setLayout(new FlowLayout());
		this.setSize(new Dimension(200, 180));
		this.add(getGroupPanel(), null);
	}

	/**
	 * This method initializes userPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getUserPanel() {
		if (userPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(2);
			gridLayout.setColumns(1);
			alertLabel = new JLabel();
			alertLabel.setHorizontalAlignment(SwingConstants.CENTER);
			alertLabel.setFont(new Font("Dialog", Font.BOLD, 12));
			alertLabel.setHorizontalTextPosition(SwingConstants.CENTER);
			usernameLabel = new JLabel();
			usernameLabel.setText("Usuário");
			usernameLabel.setName("usernameLabel");
			passwordLabel = new JLabel();
			passwordLabel.setText("Senha");
			userPanel = new JPanel();
			userPanel.setName("userPanel");
			userPanel.setLayout(gridLayout);
			userPanel.add(usernameLabel, null);
			userPanel.add(getUsernameField(), null);
		}
		return userPanel;
	}

	/**
	 * This method initializes usernameField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getUsernameField() {
		if (usernameField == null) {
			usernameField = new JTextField();
			usernameField.setColumns(15);
			usernameField.setName("usernameField");
		}
		return usernameField;
	}
	
	/**
	 * This method initializes passwordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setColumns(15);
		}
		return passwordField;
	}

	/**
	 * This method initializes loginButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getLoginButton() {
		if (loginButton == null) {
			loginButton = new JButton();
			loginButton.setText("Login");
			loginButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					boolean flag = false;
					String msgLogin = "Verifique usuário ou senha";
					try {
						flag = ClientMediator.exec("UsersAccount.login",
								new Object[] { usernameField.getText(),
										passwordField.getText() });
					} catch (XmlRpcException ex) {
						// Logger.getLogger(Login.class.getName()).log(Level.SEVERE,
						// null, ex);
						msgLogin = "Verifique a conexão!";
					}

					if (flag) {
						ChangerView.changeView(ChangerView.CONTACTLIST);
						ChangerView.getJFrame().setSize(300, 500);
						alertLabel.setText("");
					} else {
						alertLabel.setText(msgLogin);
					}
				}
			});
		}
		return loginButton;
	}

	/**
	 * This method initializes passPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPassPanel() {
		if (passPanel == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(2);
			gridLayout1.setColumns(3);
			passPanel = new JPanel();
			passPanel.setName("passPanel");
			passPanel.setLayout(gridLayout1);
			passPanel.add(passwordLabel, null);
			passPanel.add(getPasswordField(), null);
		}
		return passPanel;
	}

	/**
	 * This method initializes commandPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getCommandPanel() {
		if (commandPanel == null) {
			GridLayout gridLayout2 = new GridLayout();
			gridLayout2.setRows(2);
			gridLayout2.setColumns(1);
			commandPanel = new JPanel();
			commandPanel.setName("commandPanel");
			commandPanel.setLayout(gridLayout2);
			commandPanel.add(alertLabel, null);
			commandPanel.add(getLoginButton(), null);
		}
		return commandPanel;
	}

	/**
	 * This method initializes groupPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getGroupPanel() {
		if (groupPanel == null) {
			GridLayout gridLayout4 = new GridLayout();
			gridLayout4.setRows(3);
			gridLayout4.setColumns(1);
			groupPanel = new JPanel();
			groupPanel.setName("jPanel");
			groupPanel.setLayout(gridLayout4);
			groupPanel.add(getUserPanel(), null);
			groupPanel.add(getPassPanel(), null);
			groupPanel.add(getCommandPanel(), null);
		}
		return groupPanel;
	}

}  //  @jve:decl-index=0:visual-constraint="15,0"
